const { findGoodsList } = require("../../../utils/api")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsLists: [],//商品分页数据
        page: 1,
        limit: 10,
        status: 'more',
        category2Id:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getGoodsList();
        options.category2Id &&  this.setData({
          category2Id:options.category2Id
        })
    },
    async getGoodsList() {
        try {
            //滑到最底部的话，展示一个loading的状态，然后再发请求
            this.setData({
                status: 'loading',
            })
            let { page, limit, goodsLists,category2Id } = this.data;
            let result = await findGoodsList({ limit, page,category2Id });
            // console.log(result);
            if (result.code === 200) {
                //数据为成功后，再去考虑每页展示的数据条数
                if (result.data.records.length > 0) {
                    if (result.data.records.length < 10) {
                        this.setData({
                            status: 'nomore'
                        })
                    } else {
                        this.setData({
                            status: 'more'
                        })
                    }
                    goodsLists.push(...result.data.records)
                    this.setData({
                        goodsLists,
                        page: ++page,
                    })
                } else {
                    this.setData({
                        status: 'nomore'
                    })
                }
            }
        } catch (error) {
            console.log(error);
            this.setData({
                status: 'error'
            })
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.data.status === 'more' && this.getGoodsList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})